﻿using System.Collections.Generic;
using System.Linq;

namespace SiteFox.Business.Manager
{ 
    public static class SiteMapNode
    {
        public static IList<Model.SiteMapNode> GetRootNodes()
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.SiteMapNode where c.ID_SiteMapNode == null orderby c.Ordering select c;
            return _Query.ToList();
        }
        public static IList<Model.SiteMapNode> GetByParentNode(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.SiteMapNode where c.ID_SiteMapNode == _ID orderby c.Ordering select c;
            return _Query.ToList();
        }
        public static Model.SiteMapNode GetByID(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.SiteMapNode where c.ID == _ID select c;
            return _Query.First();
        }
        public static void Create(Model.SiteMapNode _SiteMapNode)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            _Entities.AddToSiteMapNode(_SiteMapNode);
            _Entities.SaveChanges();
        }
        public static void Edit(Model.SiteMapNode _SiteMapNode)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.SiteMapNode where c.ID == _SiteMapNode.ID select c;
            var __SiteMapNode = _Query.Single();
            __SiteMapNode.Description = _SiteMapNode.Description;
            __SiteMapNode.ImageUrl = _SiteMapNode.ImageUrl;
            __SiteMapNode.Ordering = _SiteMapNode.Ordering;
            __SiteMapNode.Target = _SiteMapNode.Target;
            __SiteMapNode.Title = _SiteMapNode.Title;
            __SiteMapNode.Url = _SiteMapNode.Url;
            _Entities.SaveChanges();
        }
        public static void Delete(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.SiteMapNode where c.ID == _ID select c;
            var _SiteMapNode = _Query.First();
            DeleteChildNodes(_ID);
            _Entities.SiteMapNode.DeleteObject(_SiteMapNode);
            _Entities.SaveChanges();
        }
        private static void DeleteChildNodes(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.SiteMapNode where c.ID_SiteMapNode == _ID select c;
            var _SiteMapNodes = _Query.ToList();
            for (int i = 0; i < _SiteMapNodes.Count; i++)
            {
                var _SiteMapNode = _SiteMapNodes[i];
                DeleteChildNodes(_SiteMapNode.ID);
                _Entities.SiteMapNode.DeleteObject(_SiteMapNode);
                _Entities.SaveChanges();
            }
        }
    }
}
